#   This program is free software; you can redistribute it and/or modify
#   it under the terms of the version 3 of the GNU Lesser General Public License
#   as published by the Free Software Foundation.
#
#   This program is distributed in the hope that it will be useful,
#   but WITHOUT ANY WARRANTY; without even the implied warranty of
#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#   GNU General Public License for more details.
#
#   You should have received a copy of the GNU Lesser General Public License
#   along with this program; if not, write to the Free Software
#   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
#
# Copyright (c) NEC Deutschland GmbH, NEC HPC Europe
#
# $Id$

import os, unittest
from time import time

from log import LOG, NS_IN_S


class LOGTest(unittest.TestCase):
    def setUp( self ):
        self.tearDown()
        self.log = LOG( "test.log" )

    def tearDown( self ):
        try: os.unlink( "test.log" )
        except: pass


class LOGAppendTest(LOGTest):
    def runTest( self ):
        self.log.append( "TEST", "test oputput" )


class LOGFetchAllTest(LOGTest):
    def runTest( self ):
        self.log.append( "A", "a output" )
        records = self.log.fetch_all()
        assert len( records ) == 1
        assert records[0].value == "A"
        assert records[0].output == "a output"
        self.log.append( "B", "b output" )
        records = self.log.fetch_all()
        assert len( records ) == 2
        assert records[0].value == "A"
        assert records[1].value == "B"
        assert records[0].output == "a output"
        assert records[1].output == "b output"


class LOGFetchFromTest(LOGTest):
    def runTest( self ):
        start_time_ns = long( time() * NS_IN_S ) 
        self.log.append( "C" )
        records = self.log.fetch_from( start_time_ns )
        assert len( records ) == 1
        self.log.append( "D" )
        records = self.log.fetch_from( start_time_ns )
        assert len( records ) == 2


class LOGFetchRangeTest(LOGTest):
    def runTest( self ):
        start_time_ns = long( time() * NS_IN_S ) 
        self.log.append( "A" )
        end_time_ns = long( time() * NS_IN_S )
        records = self.log.fetch_range( start_time_ns, end_time_ns )
        assert len( records ) == 1, "len( records ) == " + str( len( records ) )
        self.log.append( "B" )
        end_time_ns = long( time() * NS_IN_S )
        records = self.log.fetch_range( start_time_ns, end_time_ns )
        assert len( records ) == 2


class LOGFetchUntilTest(LOGTest):
    def runTest( self ):
        self.log.append( "E" )
        end_time_ns = long( time() * NS_IN_S )
        records = self.log.fetch_until( end_time_ns )
        assert len( records ) == 1
        self.log.append( "F" )
        end_time_ns = long( time() * NS_IN_S )
        records = self.log.fetch_until( end_time_ns )
        assert len( records ) == 2


suite = unittest.TestSuite()
suite.addTest( LOGAppendTest() )
suite.addTest( LOGFetchAllTest() )
suite.addTest( LOGFetchFromTest() )
suite.addTest( LOGFetchRangeTest() )
suite.addTest( LOGFetchUntilTest() )


if __name__ == "__main__":
    unittest.TextTestRunner().run( suite )
